package com.cloud.school.bus.teacherhelper.base.camera;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.utils.SDCardToolUtil;
import com.cloud.school.bus.teacherhelper.base.database.AllPictureIgnoreDB;
import com.cloud.school.bus.teacherhelper.base.database.TeacherPictureIgnoreDB;
import com.cloud.school.bus.teacherhelper.base.database.UploadFileDB;
import com.cloud.school.bus.teacherhelper.base.database.VideoIgnoreDB;
import com.cloud.school.bus.teacherhelper.base.uploadpic.UploadPic;
import com.cloud.school.bus.teacherhelper.base.utils.PictureUtil;
import com.cloud.school.bus.teacherhelper.entitys.Picture;

import android.R.string;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.media.Image;
import android.net.Uri;
import android.os.Debug;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.Thumbnails;
import android.provider.MediaStore.Video;

public final class CacheService {

	public static final String[] IMAGE_PROJECTION = new String[] {
			Images.ImageColumns._ID, Images.ImageColumns.DATE_MODIFIED,
			Images.ImageColumns.DATA };

	public static final String[] THUMB_PROJECTION = { Thumbnails._ID,
			Thumbnails.IMAGE_ID, Thumbnails.DATA };

	public static final List<Picture> getImageList(final Context context) {
		AllPictureIgnoreDB allPictureIgnoreDB = AllPictureIgnoreDB
				.getAllPictureIgnoreDB(context);
		List<Picture> ignoreList = allPictureIgnoreDB.getPictureList();

		List<Picture> pictures = new ArrayList<Picture>();
		final Uri uriImages = Images.Media.EXTERNAL_CONTENT_URI;
		final Uri uriThumbImages = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;

		final ContentResolver cr = context.getContentResolver();
		Cursor cursorImages = null;
		try {
			cursorImages = cr.query(uriImages, IMAGE_PROJECTION, null, null,
					null);

			if (cursorImages != null && cursorImages.moveToFirst()) {
				final int size = cursorImages.getCount();
				// DebugLog.logI("Images.Media.EXTERNAL_CONTENT_URI : " + size);
				do {
					if (Thread.interrupted()) {
						break;
					}

					Picture picture = new Picture();

					int id = cursorImages
							.getColumnIndexOrThrow(Images.ImageColumns._ID);
					String idString = cursorImages.getString(id);
					picture.pictureIds = idString;
					// DebugLog.logI("id : " + idString);

					Cursor cursorThumb = null;
					try {
						cursorThumb = context.getContentResolver()
								.query(uriThumbImages,// 指定缩略图数据库的Uri
										THUMB_PROJECTION,// 指定所要查询的字段
										MediaStore.Images.Thumbnails.IMAGE_ID
												+ " = ?",// 查询条件
										new String[] { idString }, // 查询条件中问号对应的值
										null);
						cursorThumb.moveToFirst();
						int thumbId = cursorImages
								.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
						String thumbIdString = cursorThumb.getString(thumbId);
						picture.thumbIds = thumbIdString;
						// DebugLog.logI("thumbIdString : " + thumbIdString);

						int thumbIData = cursorImages
								.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA);
						String thumbIDataString = cursorThumb
								.getString(thumbIData);
						picture.thumbPath = thumbIDataString;
						// DebugLog.logI("thumbIDataString : " +
						// thumbIDataString);

						int thumbImageId = cursorImages
								.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID);
						String imageIdStrigng = cursorThumb
								.getString(thumbImageId);
						System.out
								.println("imageIdStrigng : " + imageIdStrigng);

					} catch (Exception e) {
//						e.printStackTrace();
					} finally {
						if(null != cursorThumb){
							cursorThumb.close();
							cursorThumb = null;
						}
					}

					// int dateModify = cursorImages
					// .getColumnIndexOrThrow(Images.ImageColumns.DATE_MODIFIED);
					// String dateModifyString = cursorImages
					// .getString(dateModify);
					// System.out
					// .println("dateModifyString : " + dateModifyString);

					int data = cursorImages
							.getColumnIndexOrThrow(Images.ImageColumns.DATA);
					String dataString = cursorImages.getString(data);
					picture.setPicturePath(dataString);
					// DebugLog.logI("picPathString : " + dataString);

					// DebugLog.logI("----------------------------------------------------------");

					// 临时的用于过滤忽略的
					Picture tempPicture = new Picture();
					tempPicture.setPicturePath(Uri.decode(Uri.fromFile(
							new File(dataString)).toString()));

					if (!ignoreList.contains(tempPicture)) {
						pictures.add(0,picture);
					}
				} while (cursorImages.moveToNext());

			}

		} catch (Exception e) {
			// If the database operation failed for any reason
//			e.printStackTrace();
		} finally {
			if (null != cursorImages) {
				cursorImages.close();
				cursorImages = null;
			}
		}

		allPictureIgnoreDB.close();
		return pictures;
	}

	/**
	 * 获取教师助手目录下的照片
	 * 
	 * @param context
	 * @return
	 */
	public static List<Picture> getTeatcherPictures(Context context) {
		List<Picture> pictures = new ArrayList<Picture>();
		if (SDCardToolUtil.isExistSDCard()) {
			TeacherPictureIgnoreDB teacherPictureIgnoreDB = TeacherPictureIgnoreDB
					.getTeacherPictureIgnoreDB(context);
			List<Picture> ignoreList = teacherPictureIgnoreDB.getPictureList();
			String teacherPicture = UploadPic.SDCARD_ROOT_PATH + "/"
					+ UploadPic.DCIM + "/" + UploadPic.APP_PIC_FOLDER + "/";
			pictures = PictureUtil.getFiles(teacherPicture, ignoreList);
		} else {
			HandlerToastUI.getHandlerToastUI(context, "请插入sd卡");
		}

		return pictures;
	}

	/**
	 * 获取教师助手目录下的视频
	 * 
	 * @param context
	 * @return
	 */
	public static List<Picture> getTeatcherVideo(Context context) {
		List<Picture> pictures = new ArrayList<Picture>();
		if (SDCardToolUtil.isExistSDCard()) {
			VideoIgnoreDB videoIgnoreDB = VideoIgnoreDB
					.getVideoIgnoreDB(context);
			List<Picture> ignoreList = videoIgnoreDB.getPictureList();
			String teacherPicture = UploadPic.SDCARD_ROOT_PATH + "/"
					+ UploadPic.DCIM + "/" + UploadPic.APP_VIDEO_FOLDER + "/";
			pictures = PictureUtil.getVideoFiles(teacherPicture, ignoreList);
		} else {
			HandlerToastUI.getHandlerToastUI(context, "请插入sd卡");
		}
		return pictures;
	}

}
